package study.search;

public class InsertValueSearch {
    public static void main(String[] args) {
        int[] arr = new int[100];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }

        int i = insertValueSearch(arr, 0, arr.length - 1, 3);

        System.out.println(i);

    }

    public static int insertValueSearch(int[] arr, int left, int right, int findValue){

        //未找到的情况
        if (left > right || findValue < arr[0] || findValue > arr[arr.length-1]){
            return -1;
        }

        //计算中间值
        //主要是这个公式
        int midIndex = left + (right - left) * (findValue - arr[left]) / (arr[right] - arr[left]);
        int midValue = arr[midIndex];



        //找到的情况
        if (findValue < midValue){
            //扫描左边
            return insertValueSearch(arr, left, midIndex - 1, findValue);
        }else if (findValue > midValue){
            //扫描右边
            return insertValueSearch(arr, midIndex + 1, right, findValue);
        }else {
            return midIndex;
        }

    }
}
